@ECHO OFF

CD %~pd0\..\
set /p model=Select MCU type:1-F320_1(16k), 2-F326_7(16k), 3-F34x(32k), 4-F34x(64k), 5-F38x(32k), 6-F38x(64k), A-All types 
ECHO.

if %model% == 1 CALL :BUILD_F320_1
if %model% == 2 CALL :BUILD_F326_7
if %model% == 3 CALL :BUILD_F34x_32k
if %model% == 4 CALL :BUILD_F34x_64k
if %model% == 5 CALL :BUILD_F38x_32k
if %model% == 6 CALL :BUILD_F38x_64k
if %model% == A CALL :BUILD_ALL

GOTO END


:BUILD_ALL
ECHO %DATE% %TIME%       >  %~pd0\USB_BL_PARSE_CODE_BLOCK_LEN_LOG.TXT
ECHO Parsing F320_1 ...
CALL :BUILD_F320_1       >> %~pd0\USB_BL_PARSE_CODE_BLOCK_LEN_LOG.TXT
ECHO OK

ECHO Parsing F326_7 ...
CALL :BUILD_F326_7       >> %~pd0\USB_BL_PARSE_CODE_BLOCK_LEN_LOG.TXT
ECHO OK

ECHO Parsing F34x_32k ...
CALL :BUILD_F34x_32k     >> %~pd0\USB_BL_PARSE_CODE_BLOCK_LEN_LOG.TXT
ECHO OK

ECHO Parsing F34x_64k ...
CALL :BUILD_F34x_64k     >> %~pd0\USB_BL_PARSE_CODE_BLOCK_LEN_LOG.TXT
ECHO OK

ECHO Parsing F38x_32k ...
CALL :BUILD_F38x_32k     >> %~pd0\USB_BL_PARSE_CODE_BLOCK_LEN_LOG.TXT
ECHO OK

ECHO Parsing F38x_64k ...
CALL :BUILD_F38x_64k     >> %~pd0\USB_BL_PARSE_CODE_BLOCK_LEN_LOG.TXT
ECHO OK
ECHO All done!  See USB_BL_PARSE_CODE_BLOCK_LEN_LOG.TXT for details.
MOVE /Y USB_BL_PARSE_CODE_BLOCK_LEN_LOG.TXT .\Auto_gen_parse_code_len\
CALL cleanup.bat
GOTO :EOF



:BUILD_F320_1
ECHO Build F320_1 ...
CALL .\Scripts\USB_BL_Build_F320_16K.bat FREE_MODE
ECHO Build F320_1 done
REN USB_BL_F320_1_16K.M51 USB_BL_FREE_MODE.M51
CALL :PARSE_CODE
REN USB_BL_FREE_MODE.M51  USB_BL_F320_1_16K.M51
REN USB_BL_CODE_BLOCK_LEN.txt   USB_BL_F320_1_16K_CODE_BLOCK_LEN.txt
MOVE /Y USB_BL_F320_1_16K*  .\Auto_gen_parse_code_len\
GOTO :EOF


:BUILD_F326_7
ECHO Build F326_7 ...
CALL .\Scripts\USB_BL_Build_F326_16K.bat FREE_MODE
ECHO Build F326_7 done
REN USB_BL_F326_7_16K.M51 USB_BL_FREE_MODE.M51
CALL :PARSE_CODE
REN USB_BL_FREE_MODE.M51  USB_BL_F326_7_16K.M51
REN USB_BL_CODE_BLOCK_LEN.txt   USB_BL_F326_7_16K_CODE_BLOCK_LEN.txt
MOVE /Y USB_BL_F326_7_16K*  .\Auto_gen_parse_code_len\
GOTO :EOF




:BUILD_F34x_32k
ECHO Build F34x_32k ...
CALL .\Scripts\USB_BL_Build_F34X_32K.bat FREE_MODE
ECHO Build F34x_32k done
REN USB_BL_F34X_32K.M51 USB_BL_FREE_MODE.M51
CALL :PARSE_CODE
REN USB_BL_FREE_MODE.M51  USB_BL_F34X_32K.M51
REN USB_BL_CODE_BLOCK_LEN.txt   USB_BL_F34X_32K_CODE_BLOCK_LEN.txt
MOVE /Y USB_BL_F34X_32K*  .\Auto_gen_parse_code_len\
GOTO :EOF



:BUILD_F34x_64k
ECHO Build F34x_64k ...
CALL .\Scripts\USB_BL_Build_F34X_64K.bat FREE_MODE
ECHO Build F34x_64k done
REN USB_BL_F34X_64K.M51 USB_BL_FREE_MODE.M51
CALL :PARSE_CODE
REN USB_BL_FREE_MODE.M51  USB_BL_F34X_64K.M51
REN USB_BL_CODE_BLOCK_LEN.txt   USB_BL_F34X_64K_CODE_BLOCK_LEN.txt
MOVE /Y USB_BL_F34X_64K*  .\Auto_gen_parse_code_len\
GOTO :EOF



:BUILD_F38x_32k
ECHO Build F38x_32k ...
CALL .\Scripts\USB_BL_Build_F38X_32K.bat FREE_MODE
ECHO Build F38x_32k done
REN USB_BL_F38X_32K.M51 USB_BL_FREE_MODE.M51
CALL :PARSE_CODE
REN USB_BL_FREE_MODE.M51  USB_BL_F38X_32K.M51
REN USB_BL_CODE_BLOCK_LEN.txt   USB_BL_F38X_32K_CODE_BLOCK_LEN.txt
MOVE /Y USB_BL_F38X_32K*  .\Auto_gen_parse_code_len\
GOTO :EOF





:BUILD_F38X_64k
ECHO Build F38x_64k ...
CALL .\Scripts\USB_BL_Build_F38X_64K.bat FREE_MODE
ECHO Build F38x_64k done
REN USB_BL_F38X_64K.M51 USB_BL_FREE_MODE.M51
CALL :PARSE_CODE
REN USB_BL_FREE_MODE.M51  USB_BL_F38X_64K.M51
REN USB_BL_CODE_BLOCK_LEN.txt   USB_BL_F38X_64K_CODE_BLOCK_LEN.txt
MOVE /Y USB_BL_F38X_64K*  .\Auto_gen_parse_code_len\
GOTO :EOF


REM all done
goto :END



:PARSE_CODE
ECHO Pare M51 file ...
CD %~pd0\..\
GREP  OMF  USB_BL_FREE_MODE.M51 |GREP   TABLE  												   > USB_BL_CODE_BLOCK_LEN.txt
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_HANDLE_SETUP?USB_API  					   >>USB_BL_CODE_BLOCK_LEN.txt
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_USB_ISR?USB_API   						   >>USB_BL_CODE_BLOCK_LEN.txt
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?_BLOCK_WRITE?BLOCK_WRITE   						   >>USB_BL_CODE_BLOCK_LEN.txt
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_VENDOR_USB_API?USB_API					   >>USB_BL_CODE_BLOCK_LEN.txt
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?_BLOCK_READ?BLOCK_READ                                >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_GET_DESCRIPTOR?USB_API                       >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_GET_STATUS?USB_API                           >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USB_SUSPEND?USB_SUSPEND                               >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?CO?USB_API                                               >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_SET_CONFIGURATION?USB_API                    >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_CLEAR_FEATURE?USB_API                        >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_SET_FEATURE?USB_API                          >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_GET_CONFIGURATION?USB_API                    >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_USB_RESET?USB_API                            >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_GET_INTERFACE?USB_API                        >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USB_DISABLE?USB_DISABLE                               >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_JUMP_ISR?USB_JUMP_ISR                        >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_SET_ADDRESS?USB_API                          >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_FORCE_STALL?USB_API                          >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USB_INT_ENABLE?USB_INT_ENABLE                         >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_SET_SOP?USB_API                              >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?GET_INTERRUPT_SOURCE?GET_INTERRUPT_SOURCE             >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USB_INT_DISABLE?USB_INT_DISABLE                       >>USB_BL_CODE_BLOCK_LEN.txt         
ECHO             86  =DEC2HEX(D186)  ?CO?USB_BL_USB_DESCRIPTOR                                 >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?_USB_INIT?USB_API                                     >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USB_CLOCK_START?USB_CLOCK_START                       >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USBXCORE_CLKREC?USB_CLOCK_RECOVERY                    >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?PR?USB_GET_LIBRARY_VERSION?USB_GET_LIBRARY_VERSION       >>USB_BL_CODE_BLOCK_LEN.txt         
GREP  CODE USB_BL_FREE_MODE.M51 |GREP   ?C_INITSEG                                                >>USB_BL_CODE_BLOCK_LEN.txt         


ECHO.                                                     
ECHO Pare M51 file done.                                  
ECHO                                                      
TYPE USB_BL_CODE_BLOCK_LEN.txt
GOTO :EOF

:END
cd %~pd0
ECHO All Done
PAUSE


